﻿using System;
using System.Linq;
using Shell.DomainDot;
using Framework.Poco;
using System.Collections.Generic;

namespace Framework.Domain
{
    public class UserRegisterContext : DomainDotBase
    {
        public WithAll<UserContext, User> UserContext { private get; set; }

        public WithAll<User> Users { private get; set; }

        public void Register(User user, Contact contact)
        {
            user.Password = EncryptionHelper.Md5Hash(user.Password);
            user.Created = DateTime.Now;
            user.MarryTime = DateTime.Now;
            var instance = this.UserContext.Create(user);
            instance.Create(contact);
        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        public IEnumerable<User> GetAllUsers()
        {
            return this.Users;
        }

        /// <summary>
        /// 根据一个名称找出这个经销商
        /// </summary>
        public User FindNameUser(string name)
        {
            var user = this.Users.SingleOrDefault(a => a.Name == name);
            if (user != null)
            {
                return user;
            }
            return null;
        }
    }
}
